package com.erp.controller.category;

import com.erp.bean.Category;
import com.erp.dao.CategoryDao;
import com.erp.utils.ExceptionUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Author lqa
 * @Date 2022-06-01 16:20:43
 */
public class AddCategoryServlet extends HttpServlet {
    CategoryDao categoryDao = new CategoryDao();
    ExceptionUtil exceptionUtil = new ExceptionUtil();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;chartset=UTF-8");
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        String desciption = req.getParameter("desciption");
        String mark = req.getParameter("mark");
        Category category = new Category();
        category.setName(name);
        category.setDesciption(desciption);
        category.setMark(mark);
        category.setCreatedTime(LocalDateTime.now());

        if (categoryDao.save(category)) {
            resp.sendRedirect(req.getContextPath() + "/doCategoryList");
        } else {
            exceptionUtil.error("保存失败！", "doCategoryList", this.getServletContext(), req, resp);

        }
    }
}